/**
 * 
 */
package cn.jy.erp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.erp.domain.Assemble;
import cn.jy.erp.domain.Component;
import cn.jy.erp.domain.ComponentResult;
import cn.jy.erp.domain.SBPrice;
import cn.jy.erp.domain.SalePurchase;
import cn.jy.erp.domain.ZjkMiddlelibrarydetail;
import cn.jy.erp.domain.mobile.Partsmade;
import cn.jy.erp.domain.mobile.PartsmadeDetail;
import cn.jy.erp.domain.mobile.Person;
import cn.jy.erp.mapper.HydropowerMapper;
import cn.jy.erp.mapper.mobile.PartsmadeDetailMapper;
import cn.jy.erp.mapper.mobile.PartsmadeMapper;
import cn.jy.erp.service.AssembleService;
import cn.jy.erp.service.ComponertService;
import cn.jy.erp.service.FnancialPlansService;
import cn.jy.erp.service.MobilePartsmadeService;

/**
 * @author 刘中斐
 *@date 2018年8月5日 下午6:39:49 
 */
@Controller
public class ComponentController {
	@Autowired
	ComponertService componertService;
	@Autowired
	AssembleService assembleService;
	@Autowired
	MobilePartsmadeService mobilePartsmadeService;
	
	@RequestMapping("lj/cost")
	public @ResponseBody Map<String, Object>  getGjzDzJwd(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String dateTime=request.getParameter("dateTime");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(componertService.getCost(dateTime));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return componertService.getCost(dateTime);

	}
	@RequestMapping("lj/ColumnarDate")
	public @ResponseBody List<ComponentResult> getColumnarDate(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");

		String detailname=request.getParameter("detailname");
/*		if (detailname!=null) {
			detailname=new String(detailname.getBytes("ISO-8859-1"), "UTF-8");
		}*/
		System.out.println(detailname);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(componertService.getColumnarDate(detailname));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return componertService.getColumnarDate(detailname);

	}

	@RequestMapping("lj/PartsName")
	public @ResponseBody List<String> getPartsName(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(componertService.getPartsName());
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return componertService.getPartsName();
	}
	/*	@RequestMapping("lj/updatePrice")
	public void updatePrice(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		 Map<String, String[]> parameterMap = request.getParameterMap();
		 Iterator<Entry<String, String[]>> entrySet = parameterMap.entrySet().iterator();
		 while (entrySet.hasNext()) {
			 Entry<String, String[]> next = entrySet.next();
			   System.out.println(next.getKey()+"K"); 
			   System.out.println(next.getValue()+"V");
		}
		System.out.println(callback);
		for (SBPrice str : list) {
			System.out.println(str.getWLName());
			System.out.println(str.getPrice());
		}
		String WLName=request.getParameter("WLName");
		String Price=request.getParameter("Price");
		SBPrice sb=new SBPrice();
		sb.setWLName(WLName);
		sb.setPrice(Double.valueOf(Price));			
		componertService.updateSbPrice(sb);
		System.out.println("修改成功");


		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(null);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}*/
	@RequestMapping("lj/updatePrice")
	public void updatePrice(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String wlName=request.getParameter("wlName");
		String wlPrice=request.getParameter("wlPrice");
		String rgName=request.getParameter("rgName");
		String rgPrice=request.getParameter("rgPrice");
		String zjName=request.getParameter("zjName");
		String zjPrice=request.getParameter("zjPrice");


		SBPrice sb=new SBPrice();
		if (wlName!=null&&!"".equals(wlName)) {
			sb.setWLName(wlName);
			sb.setPrice(Double.valueOf(wlPrice));			
			componertService.updateSbPrice(sb);
			System.out.println("物料修改成功");
		}

		if (rgName!=null&&!"".equals(rgName)) {
			sb.setWLName(rgName);
			sb.setPrice(Double.valueOf(rgPrice));			
			componertService.updateSbPrice(sb);
			System.out.println("人工修改成功");
		}
		if (zjName!=null&&!"".equals(zjName)) {
			sb.setWLName(zjName);
			sb.setPrice(Double.valueOf(zjPrice));			
			componertService.updateSbPrice(sb);
			System.out.println("折旧修改成功");
		}


		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(null);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	@RequestMapping("zz/getByWokeEffect")
	public @ResponseBody Map<String, Object> getByWokeEffect(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String BATCHNUMS=request.getParameter("BATCHNUMS");


		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(assembleService.getByWokeEffect(BATCHNUMS));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return assembleService.getByWokeEffect(BATCHNUMS);

	}

	@RequestMapping("zz/getSameBatch")
	public @ResponseBody Map<String, Object> getSameBatch(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String BATCHNUMS=request.getParameter("BATCHNUMS");
		String Operator=request.getParameter("Operator");
		String other=request.getParameter("other");
	/*	if (Operator!=null) {
			Operator=new String(Operator.getBytes("ISO-8859-1"), "UTF-8");
		}*/
		//Map<String, Object> map=assembleService.getSameBatch(BATCHNUMS, Operator);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(assembleService.getSameBatch(BATCHNUMS,Operator,other));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return assembleService.getSameBatch(BATCHNUMS,Operator,other);

	}

	@RequestMapping("zz/getNumber")
	public @ResponseBody List<Assemble> getNumber(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String BATCHNUMS=request.getParameter("BATCHNUMS");


		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(assembleService.getNumber(BATCHNUMS));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return assembleService.getNumber(BATCHNUMS);

	}
	@RequestMapping("zz/getOperator")
	public Set<String> getOperator(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String BATCHNUMS=request.getParameter("BATCHNUMS");

		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(assembleService.getOperator(BATCHNUMS));
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return assembleService.getOperator(BATCHNUMS);

	}

	@RequestMapping("zz/getTranslate")
	public @ResponseBody List<String> getTranslate(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(assembleService.getTranslate());
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return assembleService.getTranslate();

	}


	@RequestMapping("lj/getPrice")
	public @ResponseBody List<SBPrice> getPrice(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(componertService.getPrice());
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return componertService.getPrice();

	}


	/*@RequestMapping("lj/gethyDrop")
	public List<SBPrice> gethyDrop(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(assembleService.gethyDropPower());
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return null;

	}*/
	/**
	 * 获取流水号、tid等列表、用户可以通过该列表查看要在哪个批号里添加或者修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("mobile/getTid")
	public @ResponseBody List<Partsmade> getTid(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		List<Partsmade> byIdList = mobilePartsmadeService.getByIdList();
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(byIdList);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return byIdList;

	}
/**
 * 往零件制造附表写入数据
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
	@RequestMapping("mobile/savePartsmadeDetail")
	public void savePartsmadeDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");

		String tid = request.getParameter("tid");

		String detailname = request.getParameter("detailname");

		String productbatchnums = request.getParameter("productbatchnums");

		String batchnums = request.getParameter("batchnums");

		String type = request.getParameter("type");

		String plannums = request.getParameter("plannums");

		String planunit = request.getParameter("planunit");

		String planboxnums = request.getParameter("planboxnums");

		String customer = request.getParameter("customer");

		String begintime = request.getParameter("begintime");

		String beginaddtime = request.getParameter("beginaddtime");

		String needer = request.getParameter("needer");

		String neednums = request.getParameter("neednums");

		String needunit = request.getParameter("needunit");

		String needboxnums = request.getParameter("needboxnums");

		String exceptionnums = request.getParameter("exceptionnums");

		String exceptionunit = request.getParameter("exceptionunit");

		String exceptionboxnums = request.getParameter("exceptionboxnums");

		String starttime = request.getParameter("starttime");

		String endtime = request.getParameter("endtime");

		String douliaotuiliang = request.getParameter("douliaotuiliang");

		String overaddtime = request.getParameter("overaddtime");

		String kaijidiaobiaodushu = request.getParameter("kaijidiaobiaodushu");

		String guanjidianbiaodushu = request.getParameter("guanjidianbiaodushu");

		String hegepinzongkg = request.getParameter("hegepinzongkg");

		String guanjishijian = request.getParameter("guanjishijian");

		String mouldnums = request.getParameter("mouldnums");
		
		
		String remainingkg = request.getParameter("remainingkg");
		PartsmadeDetail ps=new PartsmadeDetail();
		ps.setDid(UUID.randomUUID().toString());
		ps.setBatchnums(batchnums);
		if (beginaddtime!=null&&!"".equals(beginaddtime)) {
			ps.setBeginaddtime(beginaddtime.replace("T", " "));
		}
		if (begintime!=null&&!"".equals(begintime)) {
			ps.setBegintime(begintime.replace("T", " "));
		}
		
		ps.setCustomer(customer);
		ps.setDetailname(detailname);
		ps.setDouliaotuiliang(douliaotuiliang);
		ps.setEndtime(endtime);
		ps.setExceptionboxnums(exceptionboxnums);
		ps.setExceptionnums(exceptionnums);
		ps.setExceptionunit(exceptionunit);
		ps.setGuanjidianbiaodushu(guanjidianbiaodushu);
		if (guanjishijian!=null&&!"".equals(guanjishijian)) {
			ps.setGuanjishijian(guanjishijian.replace("T", " "));
		}
		
		ps.setHegepinzongkg(hegepinzongkg);
		ps.setKaijidiaobiaodushu(kaijidiaobiaodushu);
		ps.setMouldnums(mouldnums);
		ps.setNeedboxnums(needboxnums);
		ps.setNeeder(needer);
		ps.setNeednums(neednums);
		ps.setNeedunit(needunit);
		if (overaddtime!=null&&!"".equals(overaddtime)) {
			ps.setOveraddtime(overaddtime.replace("T", " "));
		}
		if (planboxnums!=null&&!"".equals(planboxnums)) {
			ps.setPlanboxnums(planboxnums.replace("T", " "));
		}
		
		ps.setPlannums(plannums);
		ps.setProductbatchnums(productbatchnums);
		ps.setRemainingkg(remainingkg);
		if(starttime!=null&&!"".equals(starttime)){
			ps.setStarttime(starttime.replace("T", " "));
		}
		
		ps.setTid(tid);
		ps.setType(type);
		System.out.println(type);
		ps.setPlanunit(planunit);
		System.out.println(planboxnums);
		if (ps.getTid()!=null&&!"".equals(ps.getTid())) {
			mobilePartsmadeService.savePartsmadeDetail(ps);
		}else {
			System.out.println("参数不能为空");
		}

		/*if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString("添加成功");
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
		

	}
	/**
	 * 根据tid获取零件制造附表中的数据
	 */
	
	@RequestMapping("mobile/getTidData")
	public @ResponseBody List<PartsmadeDetail> getTidData(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String tid=request.getParameter("tid");
		List<PartsmadeDetail> byIdList = mobilePartsmadeService.getByTidData(tid);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(byIdList);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return byIdList;

	}
	/**
	 * 登录验证
	 */
	@RequestMapping("mobile/getlogin")
	public @ResponseBody String getlogin(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String result=null;
		String name=request.getParameter("name");
		String passWord=request.getParameter("passWord");
//		if (name!=null) {
//			name=new String(name.getBytes("ISO-8859-1"), "UTF-8");
//		}
		List<Person> login = mobilePartsmadeService.getLogin(name);
		for (Person person : login) {
			if (person.getPERSONPASSWORD()!=null) {
				if (passWord.equals(person.getPERSONPASSWORD())) {
					result="1";
				}else {
					result="2";
				}
			}else {
				result="2";
			}
		}
		System.out.println(result);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(result);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return result;

	}
	
	/**
	 * 零件添加记录码表
	 */
	@RequestMapping("mobile/getByLjMB")
	public @ResponseBody List<String> getByLjMB(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String type=request.getParameter("type");
		List<String> byLjMB = mobilePartsmadeService.getByLjMB(type);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(byLjMB);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return byLjMB;

	}
	@RequestMapping("zjk/getOnlyName")
	public @ResponseBody List<ZjkMiddlelibrarydetail> getOnlyName(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
			List<ZjkMiddlelibrarydetail> onlyDetailName = componertService.getOnlyDetailName();
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(onlyDetailName);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return onlyDetailName;

	}
	@RequestMapping("zjk/getDetail")
	public @ResponseBody JSONObject getDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String name=request.getParameter("name");
		JSONObject zjkDetail = componertService.getZJKDetail(name);
		System.out.println(zjkDetail);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(zjkDetail);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return zjkDetail;

	}
}
